import { isNumber, some } from "lodash";
/**
 * 获取节点和线的状态是否有异常
 * 同环比判定中-只判断当前查看的是同比还是环比数据。attachDataType 0-环比 1-同比
 * 环比数据对比增长超过50%（请求量，错误率，响应时间）
 * 同比数据对比增长超过50%（请求量，错误率，响应时间）
 * 当前错误率超过50%
 * 当前响应时间超过5s
 * @param data 节点或线上数据
 * @returns 1-异常 0-正常
 */
export const getNodeAndEdgeStatus = (data) => {
  const {
    responseTimeDodRate,
    responseTimeRingRate,
    requestTotalDodRate,
    requestTotalRingRate,
    errorRateDodRate,
    errorRateRingRate,
    errorRate,
    responseTime,
    attachDataType,
    errorMessage,
  } = data;
  const dodValues = [
    responseTimeDodRate,
    requestTotalDodRate,
    errorRateDodRate,
  ];
  const ringValues = [
    responseTimeRingRate,
    requestTotalRingRate,
    errorRateRingRate,
  ];

  if (errorMessage?.length) {
    return 1;
  }

  const errorRateExceeds = isNumber(errorRate) && errorRate > 50;
  const responseTimeExceeds =
    isNumber(responseTime) && responseTime > 5000000000;
  if (attachDataType) {
    // 同比
    const dodExceeds = some(
      dodValues,
      (value) => isNumber(value) && Math.abs(value) > 50
    );
    return dodExceeds || responseTimeExceeds || errorRateExceeds ? 1 : 0;
  } else {
    // 环比
    const dodExceeds = some(
      ringValues,
      (value) => isNumber(value) && Math.abs(value) > 50
    );
    return dodExceeds || responseTimeExceeds || errorRateExceeds ? 1 : 0;
  }
};
// 清除拓扑图所有状态样式
export const clearAllStats = (graph) => {
  if (!graph) {
      return;
  }
  graph.setAutoPaint(false);
  graphFocusNode('', graph);
  graph.getNodes().forEach((node) => {
      graph.clearItemStates(node);
  });
  graph.paint();
  graph.setAutoPaint(true);
}

// 拓扑图搜索节点focus
export const graphFocusNode = (item, graph, isFocus = false) => {
  const allNodes = graph.getNodes();
  const allEdges = graph.getEdges();
  const allCombos = graph.getCombos();
  // ! 折叠起来的combo只能通过combo来查edge
  const allEdgesPlus = allCombos.reduce((prev, cur) => {
      prev.push(...cur.getEdges())
      return prev
  }, []);

  if (item) {
      const nodeId = item.getModel().id;

      [].concat(allEdgesPlus, allNodes, allEdges).forEach((item) => {
          const group = item.getContainer()
          const children = group.get('children')
          children.forEach((item) => {
              item.attr({
                  opacity: 0.2
              })
          })

      })
      // * 获取关联的边
      const relatedEdges = item.getEdges()
      //* 根据边找到所有的关联节点和边
      // ! 通过边来查找关系,会导致一个问题,没有边的节点,查不到任何节点和边
      const sourceNodes = relatedEdges.map((item) => item.getSource().getModel().id)
      const targetNodes = relatedEdges.map((item) => item.getTarget().getModel().id)
      const edges = relatedEdges.map((item) => item.getModel().id)
      // * 去重之后高亮
      Array.from(new Set([].concat(sourceNodes, targetNodes, edges, nodeId))).forEach((id) => {
          const graphItem = graph.findById(id)
          const itemChildren = graphItem.get('group').get('children')
          itemChildren.forEach((item) => {
              item.attr({ opacity: 1 })
          })
      })

      isFocus && graph.focusItem(item);
  } else {
      [].concat(allNodes, allEdges, allEdgesPlus).forEach((item) => {
          const group = item.getContainer()
          const children = group.get('children')
          children.forEach((item) => {
              item.attr({
                  opacity: 1
              });
          })
      });
  }
}
